package com.itboye.jigongbao.retrofit.util;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Base64;
import android.util.Log;

import com.google.gson.Gson;
import com.itboye.jigongbao.app.MyApplication;
import com.itboye.jigongbao.utils.Const;

import java.util.HashMap;
import java.util.Map;

/**
 * BuilMapUtils
 * <p>
 * Created by Mr.w on 2017/12/13.
 * <p>
 * 版本      ${version}
 * <p>
 * 修改时间
 * <p>
 * 修改内容
 */

//secret :byTestSecret654321
public class BuilMapUtils {

    Map<String, Object> map = new HashMap<>();

    public BuilMapUtils() {
        map = new HashMap<>();
    }

    public static String hashMapToBase64(HashMap<String, Object> object) {
        String param = new ParamDeal().dataEncrypt(object);
        return param;
    }

    public static String signInfo(String time, String typekey, String userInf, String notify_id) {

        ParamDeal operate = new ParamDeal();
        String sign = operate.getMD5Sign(time, typekey, userInf,
                Const.Companion.getSECRET(), notify_id);
        System.out.println("msg5" + sign);
        return sign;
    }

    /**
     * 字节数组转为普通字符串（ASCII对应的字符）
     *
     * @param bytearray byte[]
     * @return String
     */
    public static String bytetoString(byte[] bytearray) {
        String result = "";
        char temp;

        int length = bytearray.length;
        for (int i = 0; i < length; i++) {
            temp = (char) bytearray[i];
            result += temp;
        }
        return result;
    }

    /**
     * 获取当前时间戳
     *
     * @return int 类型数据
     */
    public static String getStamp() {
        Long tsLong = System.currentTimeMillis() / 1000;
        String ts = tsLong.toString();
        return ts;
    }

    public static int getVersionCode() {
        String versionName = "";
        int versioncode = 0;
        try {
            // ---get the package info---
            PackageManager pm = MyApplication.Companion.getInstance().getPackageManager();
            PackageInfo pi = pm.getPackageInfo(MyApplication.Companion.getInstance().getPackageName(), 0);
            versionName = pi.versionName;
            versioncode = pi.versionCode;
//            if (versionName == null || versionName.length() <= 0) {
//                return 0;
//            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versioncode;
    }

//    public Map getHashMap() {
//        int ar=args.length;
//        for (String arg : args) {
//            arg.ge
//        }
//
//        return map;
//    }

    public static class buildParams {

        private HashMap<String, Object> hashMap;
        String param;
        private String typeKey;

        public buildParams(String typeKey, HashMap<String, Object> hashMap) {
            this.hashMap = hashMap;
            this.typeKey = typeKey;
            param = hashMapToBase64(hashMap);
        }


        public buildParams addParams(String key, String value) {
            //比如传入的typKey=user_login
            hashMap.put(key, value);
            return this;
        }


        public String buildToString() {
            if (typeKey == null) {
                throw new RuntimeException("typeKey is null");
            }
            String time = getStamp();
            String notify_id = getStamp();
            String sign = signInfo(time, typeKey, param, notify_id);
            addParams(Const.Companion.getDATA(), param)
                    .addParams(Const.Companion.getSIGN(), sign)
                    .addParams(Const.Companion.getTIME(), time)
                    .addParams(Const.Companion.getNOTIFY_ID(), time);
            String jsonStr = new Gson().toJson(hashMap);
            Log.v(Const.Companion.getTAG_DEBUG(), "requestData >" + jsonStr);
            LogUtil.Companion.printJson(Const.Companion.getTAG_DEBUG(),jsonStr,"requestData");
            String desContent = DESUtil.encode(jsonStr, Const.Companion.getSECRET());
            String itboyeData = bytetoString(Base64.encode(desContent.getBytes(), Base64.DEFAULT));
            return itboyeData;
        }

    }


}
